<meta charset="utf-8">
(#) Using the result of check permission calls

!!! WARNING: Using the result of check permission calls
   This is a warning.

Id
:   `UseCheckPermission`
Summary
:   Using the result of check permission calls
Severity
:   Warning
Category
:   Security
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   3.6.0 (February 2020)
Affects
:   Kotlin and Java files and test sources
Editing
:   This check runs on the fly in the IDE editor
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/CheckResultDetector.kt)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/CheckResultDetectorTest.kt)

You normally want to use the result of checking a permission; these
methods return whether the permission is held; they do not throw an
error if the permission is not granted. Code which does not do anything
with the return value probably meant to be calling the enforce methods
instead, e.g. rather than `Context#checkCallingPermission` it should
call `Context#enforceCallingPermission`.

!!! Tip
   This lint check has an associated quickfix available in the IDE.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/CheckPermissions.java:10:Warning: The result of
checkCallingOrSelfPermission is not used; did you mean to call
#enforceCallingOrSelfPermission(String,String)? [UseCheckPermission]
    context.checkCallingOrSelfPermission(Manifest.permission.INTERNET); // WRONG
    ------------------------------------------------------------------
src/test/pkg/CheckPermissions.java:11:Warning: The result of
checkPermission is not used; did you mean to call
#enforcePermission(String,int,int,String)? [UseCheckPermission]
    context.checkPermission(Manifest.permission.INTERNET, 1, 1);
    -----------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here are the relevant source files:

`src/test/pkg/CheckPermissions.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;

@SuppressWarnings("ClassNameDiffersFromFileName")
public class CheckPermissions {
    private void foo(Context context) {
        context.checkCallingOrSelfPermission(Manifest.permission.INTERNET); // WRONG
        context.checkPermission(Manifest.permission.INTERNET, 1, 1);
        check(context.checkCallingOrSelfPermission(Manifest.permission.INTERNET)); // OK
        int check = context.checkCallingOrSelfPermission(Manifest.permission.INTERNET); // OK
        if (context.checkCallingOrSelfPermission(Manifest.permission.INTERNET) // OK
                != PackageManager.PERMISSION_GRANTED) {
            showAlert(context, "Error",
                    "Application requires permission to access the Internet");
        }
    }

    private Bitmap checkResult(Bitmap bitmap) {
        bitmap.extractAlpha(); // WARNING
        Bitmap bitmap2 = bitmap.extractAlpha(); // OK
        call(bitmap.extractAlpha()); // OK
        return bitmap.extractAlpha(); // OK
    }

    private void showAlert(Context context, String error, String s) {
    }

    private void check(int i) {
    }
    private void call(Bitmap bitmap) {
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test/pkg/Intersect.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;
import android.graphics.Rect;

@SuppressWarnings({"ClassNameDiffersFromFileName", "MethodMayBeStatic"})
public class Intersect {
  void check(Rect rect, int aLeft, int aTop, int aRight, int aBottom) {
    rect.intersect(aLeft, aTop, aRight, aBottom);
  }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/CheckResultDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `CheckResultDetector.testCheckResult`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("UseCheckPermission")
  fun method() {
     problematicStatement()
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("UseCheckPermission")
  void method() {
     problematicStatement();
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection UseCheckPermission
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="UseCheckPermission" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'UseCheckPermission'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore UseCheckPermission ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>